package edu.cuit.zhuyimeng.framework.security.security;

import cn.dev33.satoken.stp.StpInterface;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.framework.common.result.HttpStatusCodeConstants;
import edu.cuit.zhuyimeng.uaa.api.controller.AuthenticationApiController;
import edu.cuit.zhuyimeng.uaa.api.controller.VerificationApiController;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

/**
 * 鉴权实现类
 */
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final AuthenticationApiController authenticationApiController;
    private final VerificationApiController verificationApiController;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        CommonResult<List<String>> result = authenticationApiController.getPermissions();
        if (result.getCode() != HttpStatusCodeConstants.SUCCESS.getCode().intValue()) {
            throw new ResponseStatusException(HttpStatus.valueOf(result.getCode()),result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        CommonResult<List<String>> result = authenticationApiController.getRoles();
        if (result.getCode() != HttpStatusCodeConstants.SUCCESS.getCode().intValue()) {
            throw new ResponseStatusException(HttpStatus.valueOf(result.getCode()),result.getMsg());
        }
        return result.getData();
    }
}
